<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Order_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 根据用户ID返回每月下单金额的信息
	 */
	public function get_month_total_by_userid($userid){
		$query = $this->db->query("select DATE_FORMAT(order_time,'%Y-%m') as date,sum(total) as total from t_order 
															where userid=".$userid." and del=0 and DATE_FORMAT(order_time,'%Y-%m')>='2016-02' group by DATE_FORMAT(order_time,'%Y-%m')
															order by order_time desc");
		return $query->result_array();
	}
	/*
	 * 根据用户ID返回当月每天下单金额的信息
	 */
	public function get_day_total_by_userid($userid,$date){
		$this->db->select("DATE_FORMAT(order_time,'%Y-%m-%d') as date,total");
		$this->db->where('userid',$userid);
		$this->db->where('del=0');
		$this->db->where("DATE_FORMAT(order_time,'%Y-%m')",$date);
		$query = $this->db->get('t_order');
		return $query->result_array();
	}
	/*
	 * 查询已支付的有效订单数
	 */
	public function find_orders_num($userid) {
		$this->db->where('userid',$userid);
		$this->db->where('state>=',2);
		$this->db->where('del=0');
		$query = $this->db->get('t_order');
		return $query->num_rows();
	}
}